package me.yricky.mayu.desktop

import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.*
import me.yricky.mayu.desktop.AppState.fileListContent
import me.yricky.mayu.desktop.AppState.mainWindowContent
import me.yricky.mayu.desktop.AppState.seqListContent
import me.yricky.mayu.desktop.AppState.taskListContent
import me.yricky.mayu.desktop.AppState.tasksListModel
import me.yricky.mayu.desktop.AppState.workList
import me.yricky.mayu.ui.Theme
import java.awt.Dimension

fun main() {
    Theme.initLookAndFeel()
    application {
        var minimize by remember { mutableStateOf(false) }
        if(minimize){
            Tray(
                rememberVectorPainter(Icons.Rounded.CatchingPokemon),
                onAction = { minimize = false },
                tooltip = "Mayu",
                menu = {
                    Item("还原"){
                        minimize = false
                    }
                    Item("退出"){
                        exitApplication()
                    }
                }
            )
        }else{
            val mainWindowState = rememberWindowState()
            Window(
                icon = painterResource("svg/b/M.svg"),
                onCloseRequest = ::exitApplication,
                state = mainWindowState,
                title = "Mayu"
            ) {
                LaunchedEffect(Unit){ window.minimumSize = Dimension(800,600) }
                MaterialTheme(
                    colors = MaterialTheme.colors.copy(
                        primary = Color(0xff5588aa),
                        secondary = Color(0xff66cc99)
                    )
                ) {
                    Row{
                        NavigationRail {
                            NavigationRailItem(
                                selected = mainWindowContent === fileListContent,
                                onClick = { AppState.navigate(ToFileList()) },
                                icon = { Icon(Icons.Rounded.Folder, "",Modifier.size(32.dp)) },
                                label = { Text("浏览") }
                            )
                            NavigationRailItem(
                                selected = mainWindowContent === seqListContent,
                                onClick = { AppState.navigate(ToSeqList()) },
                                icon = { Icon(Icons.Rounded.Wysiwyg, "",Modifier.size(32.dp)) },
                                label = { Text("序列") }
                            )
                            NavigationRailItem(
                                selected = mainWindowContent === taskListContent,
                                onClick = { AppState.navigate(ToTaskList()) },
                                icon = { Icon(Icons.Rounded.AvTimer, "",Modifier.size(32.dp)) },
                                label = { Text("任务(${tasksListModel.list.size})") }
                            )
                            Spacer(Modifier.weight(1f))
                            NavigationRailItem(
                                selected = false,
                                onClick = { minimize = true },
                                icon = { Icon(Icons.Rounded.ExpandCircleDown, "",Modifier.size(32.dp)) },
                                label = { Text("隐藏到托盘") }
                            )
                        }
                        Crossfade(mainWindowContent){
                            it()
                        }

                    }
                    workList.forEach {
                        it.second()
                    }
                }
            }
        }
    }
}